import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-10-31
 * Time: 08:36
 */
public class Demo2 {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源头
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl(
                "jdbc:mysql://127.0.0.1:3306/java109?serverTimezone=UTC&characterEncoding=utf8&useSSL=false");//发生向下转型
        //需要写MySQL登录的用户名和密码
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.构造SQL语句
        String sql = "insert into student2 values(100,'熊二')";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行SQL
        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        //5.释放资源
        statement.close();
        connection.close();
    }
}
